package com.rop.znyx.product.config;

import io.minio.*;
import io.minio.errors.*;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;

/**
 * Created with IntelliJ IDEA
 * User: Rop
 * Date: 2023/6/25
 * Time: 15:38
 * Description:
 */
@Data
@Component
@ConfigurationProperties(prefix = "minio")
public class MinIOConfig {
    private String endPoint;

    private String accessKey;

    private String secretKey;

    private String bucketName;

    private String objectName;

    public MinioClient getMinioClient() {
        return MinioClient.builder()
                .endpoint(endPoint)
                .credentials(accessKey, secretKey)
                .build();
    }

    /**
     * MinIO分步式文件系统，文件上传
     * @param fileName 自定义文件名
     * @param file 上传的文件
     * @param inputStream 上传的文件流
     */
    public String uploadFile(String fileName, MultipartFile file, InputStream inputStream) throws ServerException, InsufficientDataException, ErrorResponseException, IOException, NoSuchAlgorithmException, InvalidKeyException, InvalidResponseException, XmlParserException, InternalException {
        // MinIO客户端
        MinioClient minioClient = this.getMinioClient();
        boolean found =
                minioClient.bucketExists(BucketExistsArgs.builder().bucket(bucketName).build());
        if (!found) {
            // Make a new bucket called 'bucketName'.
            minioClient.makeBucket(MakeBucketArgs.builder().bucket(bucketName).build());
        } else {
            System.out.println("Bucket " + bucketName + " already exists.");
        }

        PutObjectArgs putObjectArgs = PutObjectArgs.builder()
                .bucket(bucketName)
                .object(fileName)
                .stream(inputStream, file.getSize(), -1)
                .build();

        // 文件上传
        ObjectWriteResponse objectWriteResponse = minioClient.putObject(putObjectArgs);
//        System.out.println("objectWriteResponse = " + objectWriteResponse);
        inputStream.close();

        return endPoint + "/" + bucketName + "/" + fileName;
    }

    public String uploadFile(String fileName, File file, InputStream inputStream) throws ServerException, InsufficientDataException, ErrorResponseException, IOException, NoSuchAlgorithmException, InvalidKeyException, InvalidResponseException, XmlParserException, InternalException {
        // MinIO客户端
        MinioClient minioClient = this.getMinioClient();
        boolean found =
                minioClient.bucketExists(BucketExistsArgs.builder().bucket(bucketName).build());
        if (!found) {
            // Make a new bucket called 'bucketName'.
            minioClient.makeBucket(MakeBucketArgs.builder().bucket(bucketName).build());
        } else {
            System.out.println("Bucket " + bucketName + " already exists.");
        }

        PutObjectArgs putObjectArgs = PutObjectArgs.builder()
                .bucket(bucketName)
                .object(fileName)
                .stream(inputStream, file.length(), -1)
                .build();

        // 文件上传
        ObjectWriteResponse objectWriteResponse = minioClient.putObject(putObjectArgs);
        System.out.println("objectWriteResponse = " + objectWriteResponse);

        inputStream.close();

        return "http://172.16.100.76:9000/" + bucketName + "/" + fileName;
    }
}
